/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.ui.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class SpinnerAndCombo extends Composite {

	// TEST
	public static void main(String args[]) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());

		Composite mainComp = new Composite(shell, SWT.BORDER);

		mainComp.setLayout(new GridLayout());

		SpinnerAndCombo comp = new SpinnerAndCombo(mainComp, SWT.NONE);
		comp.initSpinner(-1, 100, -1);
		comp.setLayoutData(new GridData());
		comp.setValue("90.9px");

		System.out.println(comp.getValue());

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		display.dispose();
	}

	private String[] itemListUnits = { "cm", "mm", "pc", "pt", "px" };
	private Spinner spinner;
	private Combo combo;

	private Label label;

	public SpinnerAndCombo(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;
		setLayout(gridLayout);
		initGUI();
		initCombo("cm");
		layout();
	}

	public void initCombo(int index) {
		combo.select(index);
	}

	public void initCombo(String unit) {
		setCombo(unit);
	}

	public void initSpinner(int digits, int increment, int maximum) {
		if (digits >= 0)
			spinner.setDigits(digits);

		if (increment >= 0)
			spinner.setIncrement(increment);

		if (maximum >= 0)
			spinner.setMaximum(maximum);
	}

	public String getValue() {
		return ((float) spinner.getSelection())
				/ (Math.pow(10, spinner.getDigits())) + readCombo();
	}

	private void initGUI() {
		label = new Label(this, SWT.NONE);
		{
			GridData gd = new GridData();
			label.setLayoutData(gd);
		}

		spinner = new Spinner(this, SWT.NONE);
		{
			GridData gd = new GridData();
			spinner.setLayoutData(gd);
		}
		// default
		// allow 2 decimal places
		spinner.setDigits(2);
		// set the increment value to 0.10
		spinner.setIncrement(10);
		// set the maximum value to 10000
		spinner.setMaximum(1000000);

		combo = new Combo(this, SWT.NONE);
		{
			GridData gd = new GridData();
			combo.setLayoutData(gd);
			combo.setItems(itemListUnits);
		}
	}

	public void setValue(String str) {
		if (str == null)
			return;

		String number = str.replaceAll("[a-zA-Z\\s]*", "");
		String unit = str.replaceAll("-?\\d*[,.]?\\d*", "");

		if (unit == null || unit.equals(""))
			unit = "";

		double i = 0;
		if (!number.equals("")) {
			i = Double.parseDouble(number)
					* (Math.pow(10, spinner.getDigits()));
		}

		spinner.setSelection((int) i);
		setCombo(unit);
	}

	private void setCombo(String str) {
		if (str == null)
			return;

		int index = -1;
		for (int i = 0; i < combo.getItems().length; i++)
			if (combo.getItems()[i].equalsIgnoreCase(str)) {
				index = i;
				break;
			}

		if (index > -1) {
			combo.select(index);
		}
	}

	private String readCombo() {
		if (combo.getSelectionIndex() > -1) {
			return combo.getItem(combo.getSelectionIndex());
		}
		return "";
	}
}
